package com.maimao.kill.controller;

import com.maimao.api.kill.ActivityApi;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.kill.controller.base.BaseController;
import com.maimao.kill.service.ActivityService;
import com.maimao.model.common.StatusInput;
import com.maimao.model.kill.Activity;
import com.maimao.model.kill.input.ActivitySaveEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

import static com.maimao.core.constant.Constants.ONE;
import static com.maimao.core.constant.Constants.ZERO;

/**
 * @author MaoLin Wang
 * @date 2021/4/7 10:19 上午
 */
@RestController
public class ActivityController extends BaseController implements ActivityApi {

    @Autowired
    private ActivityService activityService;
    public static final LocalDateTime YEAR_2099 = LocalDateTime.of(2099, 12, 31, 0, 0);

    @PostMapping("/save")
    @Override
    public Result<?> save(@RequestBody @Validated ActivitySaveEntity activity) {
        AuthUser authUser = checkAndGetAuthUser();
        activity.setCreateUser(authUser.getRealName());
        if (activity.getStartTime() == null) {
            activity.setStartTime(LocalDateTime.now());
        }
        Result<Void> x = checkInput(activity);
        if (x != null) return x;

        activityService.save(activity);
        return Result.ok("添加成功");
    }


    /**
     * 更新
     *
     * @param activity
     * @return
     */
    @PostMapping("/update")
    @Override
    public Result<?> update(@RequestBody @Validated ActivitySaveEntity activity) {
        if (activity.getId() == null || activity.getId() < 1) {
            return Result.notValid();
        }
        checkAndGetAuthUser();
        if (activity.getStatus() == null) {
            activity.setStatus(ONE);
        }
        if (activity.getEndTime() == null) {
            activity.setEndTime(YEAR_2099);
        }
        activity.setUpdateTime(DateUtils.now());
        if (activity.getStartTime() != null) {
            if (activity.getEndTime().compareTo(activity.getStartTime()) < 0) {
                return Result.notValid("结束时间不能小于开始时间");
            }
        }

        return activityService.update(activity);
    }

    private Result<Void> checkInput(@Validated @RequestBody ActivitySaveEntity activity) {
        if (activity.getStatus() == null) {
            activity.setStatus(ONE);
        }
        activity.setCreateTime(DateUtils.now());
        if (activity.getEndTime() == null) {
            activity.setEndTime(YEAR_2099);
        }
        if (activity.getEndTime().compareTo(activity.getStartTime()) < 0) {
            return Result.notValid("结束时间不能小于开始时间");
        }

        return null;
    }

    /**
     * 进行中的活动列表
     */
    @GetMapping("/processing")
    @Override
    public Result<?> processingList() {
        List<Activity> activities = activityService.processingActivity();
        return Result.okData(activities);
    }

    /**
     * 所有活动
     */
    @GetMapping("/list")
    @Override
    public Result<?> all() {

        return Result.okData(activityService.all());
    }

    /**
     * 修改状态
     *
     * @param statusInput
     * @return
     */
    @PostMapping("/stop-begin")
    @Override
    public Result<?> updateStatus(@RequestBody @Validated StatusInput statusInput) {
        if (statusInput.getStatus().equals(ONE) || statusInput.getStatus().equals(ZERO)) {
            activityService.updateStatus(statusInput);
            if (statusInput.getStatus().equals(ONE)) {
                return Result.ok("已开启！");
            } else {
                return Result.ok("已停止！");
            }

        } else {
            return Result.notValid("非法状态");
        }
    }

}
